<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    width="80%"
    top="5%"
  >
    <div style="height: 65vh;">
      <t-form ref="form"  :data="formData" :colon="true" :rules="rules">
        <t-row style="row-gap:24px">
          <t-col :span="12">
            <t-form-item label="项目名称" name="projectName">
              <t-input clearable v-model="formData.projectName"  placeholder="请输入项目名称"></t-input>
            </t-form-item>
          </t-col>
          <t-col :span="6">
            <t-form-item label="项目级别" name="expertProjectLevel">
              <t-radio-group :options="dict.type.expert_project_level" v-model="formData.expertProjectLevel"></t-radio-group>
            </t-form-item>
          </t-col>
          <t-col :span="6">
            <t-form-item label="本人排名" name="ranking">
              <t-input clearable v-model="formData.ranking"  placeholder="请输入本人排名"></t-input>
            </t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="项目时间" name="dateRange">
              <t-date-range-picker v-model="formData.dateRange" :placeholder="['开始时间','结束时间']" clearable />
            </t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="项目摘要" name="summary">
              <t-textarea clearable placeholder="请输入项目摘要" :autosize="{minRows: 8}" :maxlength="3000" v-model="formData.summary"></t-textarea>
            </t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="关键词多个用符号间隔" name="keyWords">
              <t-input clearable v-model="formData.keyWords" placeholder="请输入关键词" />
            </t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="承担单位" name="holdCompany">
              <t-input clearable v-model="formData.holdCompany" placeholder="请输入承担单位"></t-input>
            </t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="参与模式" name="joinPattern">
              <t-radio-group v-model="formData.joinPattern" :options="dict.type.join_patter"></t-radio-group>
            </t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="项目编号" name="projectCode">
              <t-input clearable v-model="formData.projectCode"  placeholder="请输入项目编号"></t-input>
            </t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="项目年度" name="projectAnnual">
              <t-date-picker mode="year"  v-model="formData.projectAnnual" clearable allow-input />
            </t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="资助类别" name="subsidizeType">
              <t-input clearable v-model="formData.subsidizeType"  placeholder="请输入资助类别"></t-input>
            </t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="资金总数" name="expenditure">
              <t-input-adornment append="万元">
                <t-input-number :allowInputOverLimit="false" clearable
                                v-model="formData.expenditure"
                                theme="normal"
                                :min="0"></t-input-number>
              </t-input-adornment>
            </t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="立项金额" name="projectAmount">
              <t-input-adornment append="万元">
                <t-input-number :allowInputOverLimit="false" clearable
                                v-model="formData.projectAmount"
                                theme="normal"
                                :min="0"></t-input-number>
              </t-input-adornment>
            </t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="项目状态" name="expertProjectStatus">
              <t-radio-group  v-model="formData.expertProjectStatus" :options="dict.type.expert_project_stauts"></t-radio-group>
            </t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="备注" name="remark">
              <t-textarea v-model="formData.remark"  placeholder="请输入备注">
              </t-textarea>
            </t-form-item>
          </t-col>
        </t-row>
      </t-form>
    </div>
  </t-dialog>
</template>

<script>
export default {
  name: "edit-add-expert-project",
  dicts: ['expert_project_level','join_patter','expert_project_stauts'],
  props:["objId","expertId","header"],
  data() {
    return {
      visible: true,
      loading: false,
      formData:{
        projectName: '',
        startDate: '',
        endDate: '',
        expertProjectLevel: '',
        joinPattern: '',
        expenditure: null,
        projectAmount: null,
        expertProjectStauts: '',
        subsidizeType: '',
        remark: '',
        holdCompany: '',
        keyWords: '',
        dateRange: [],
        projectAnnual: '',
      },
      rules:{
        projectName: [
          {
            required: true,
            message: "项目名称不能为空",
          },
        ],
        expertProjectLevel:[
          {
            required: true,
            message: "项目级别不能为空",
          },
        ],
        ranking:[
          {
            required: true,
            message: "本人排名不能为空",
          },
        ],
        holdCompany:[
          {
            required: true,
            message: "承担单位不能为空",
          },
        ]
      }
    };
  },
  watch: {
    objId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj();
        }
      }
    }
  },
  methods: {
    getObj(){
      this.$api.zjk.expert.projectGet(this.objId).then(
        (res) => {
          this.formData = res.data;
          this.formData.dateRange = [res.data.startDate,res.data.endDate];
        }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          if (this.objId !== 0) {
            if(this.formData.dateRange.length>0){
              [this.formData.startDate,this.formData.endDate] = this.formData.dateRange;
            }
            this.$api.zjk.expert.projectEdit({
              ...this.formData,
            }).then((response) => {
              this.$message.success("修改成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          } else {
            if(this.formData.dateRange.length>0){
              [this.formData.startDate,this.formData.endDate] = this.formData.dateRange;
            }
            this.formData.expertId = this.expertId;
            this.$api.zjk.expert.projectAdd({
              ...this.formData,
            }).then((response) => {
              this.$message.success("新增成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
/deep/ .t-form__label{
  display: block !important;
}
</style>
